﻿@page "/myorders/{orderId:int}"
@inject NavigationManager NavigationManager
@inject HttpClient HttpClient
@implements IDisposable    
@inject IJSRuntime JsRuntime





<div class="main">
    @if (_invalidOrder)
    {
        <h2>Order not found</h2>
        <p>We're sorry but this order no longer exists.</p>
    }
    else if (_orderWithStatus == null)
    {
        <div class="track-order">
            <div class="track-order-title">
                <h2>
                    <text>Loading...</text>
                </h2>
                <p class="ml-auto mb-0">
                    ...
                </p>
            </div>
        </div>
    }
    else
    {
        <div class="track-order">
            <div class="track-order-title">
                <h2>
                    Order placed @_orderWithStatus.Order.CreatedTime.ToLongDateString()
                </h2>
                <p class="ml-auto mb-0">
                    Status: <strong id="ordetStatus"> @_orderWithStatus.StatusText </strong>
                </p>
            </div>
            <div class="track-order-body">
                <div class="track-order-details">
                    <div id="progressSpinner" class="spinner-grow text-danger float-right" role="status">
                        <span class="sr-only">Checking your order status...</span>
                    </div>
                    @foreach (var pizza in _orderWithStatus.Order.Pizzas)
                    {
                        <p>
                            <strong>
                                @pizza.Size
                                @pizza.Special.Name
                                (￥@pizza.GetFormattedTotalPrice())
                            </strong>
                        </p>
                    }
                </div>
            </div>
        </div>
    }

</div>


@code {
    [Parameter]
    public int OrderId { get; set; }

    OrderWithStatus _orderWithStatus;
    bool _invalidOrder = false;

    protected override async Task OnParametersSetAsync()
    {
        try
        {
            var url = $"{NavigationManager.BaseUri}orders/{OrderId}";
            _orderWithStatus = await HttpClient.GetFromJsonAsync<OrderWithStatus>(url);
        }
        catch (Exception e)
        {
            _invalidOrder = true;
            Console.Error.WriteLine(e);
        }
    }

    public void Dispose()
    {
    }

}